package com.github.dengmin.mysql.shiro.jwt;

import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.shiro.authc.AuthenticationToken;

import java.util.Date;

/**
 * @Author dengmin
 * @Created 2020/8/14 上午10:10
 */
@Data
@Accessors(chain = true)
public class JwtToken implements AuthenticationToken {
    private static final long serialVersionUID = 1L;

    private String username;
    private String salt;
    private String token;
    private Date gmtCreated;
    private Date expireDate;
    private Long expireSecond;
    private String principal;
    private String credentials;

    @Override
    public Object getPrincipal() {
        return token;
    }

    @Override
    public Object getCredentials() {
        return token;
    }

    public static JwtToken build(String token, String username, String salt, long expireSecond){
        DecodedJWT decodedJWT = JwtUtil.getJwtInfo(token);
        Date createDate = decodedJWT.getIssuedAt();
        Date expireDate = decodedJWT.getExpiresAt();
        return new JwtToken()
                .setUsername(username)
                .setToken(token)
                .setSalt(salt)
                .setGmtCreated(createDate)
                .setExpireDate(expireDate)
                .setExpireSecond(expireSecond);
    }
}
